<?php
require_once '../global.inc.php';
Database::connect($HOST, $NAME, $PWD, $DB);
$table 			= "info_activity";
// operation操作类型：add、del、query、edit
$operation 		= $_GET["op"];

//展示模板列表
if ($operation=="query") {
	//获取页面信息
	$activityName 	= $_POST["activityName"];
	if(!isset($_GET["p"])){//首次查询
		$condition = "1=1";
		$tag	   = $_GET["tag"];      //查询后的操作方式（列表，列表编辑）
		if(strlen($activityName)!=0){
			$condition .= " and subject like '%$activityName%'";
		}

		$condition .=" order by activity_id desc";
		$_SESSION["templateQryWhere"] = $condition;
		$_SESSION["tag"]			= $tag;
	}else{
		$condition = $_SESSION["templateQryWhere"];
		$tag	   = $_SESSION["tag"];
	}
	//echo $condition;

	$count			= Database::count($table,$condition);	
	$totalRows		= $count[0];				//数据总量
	$currentPage	= $_GET["p"];				//当前页面
	$pageInfo		= array();					//初始化页面信息
	$subpages 		= new SubPages($PAGE_SIZE, $totalRows, $currentPage);	//实例化分页
	$pageInfo 		= $subpages->getPageArray();

	
	//查询本页数据
	$templateArray 		= Database::findPage($table, $pageInfo["startRow"], $pageInfo["pageSize"],$condition);

	//将分页信息和本页数据写入session
	$_SESSION["pageInfoTemplate"]	= $pageInfo;
	$_SESSION["templateArray"]	= $templateArray;
	echo "<script>window.location.href='../view/activity/templateList.php';</script>";
}


//活动模板新增
if ($operation=="add") {
	$subject		= $_POST["subject"];
	$travel			= $_POST["travel"];
	$fee			= $_POST["fee"];

	Database::begin();
	$activityIdArray = Database::max("info_activity", "activity_id");
	$activityId		 = $activityIdArray[0]+1;
	//插入活动模板资料
	$sql = "INSERT INTO info_activity(activity_id,subject,travel,fee)"
		  ."VALUES ('$activityId','$subject','$travel','$fee')";
	Database::insert($sql);
	
	if(!empty($_SESSION["attachmentArray"])){
		foreach ($_SESSION["attachmentArray"] as $attachmentInfo){
			$fileName = $attachmentInfo["file_name"];
			$filePath = $attachmentInfo["file_path"];
			$sql = "INSERT INTO activity_attachment(activity_id,file_name,file_path)"
					."VALUES ('$activityId','$fileName','$filePath')";
			Database::insert($sql);
		}
	}
	Database::commit();
	Database::end();
	//清空session
	$_SESSION["attachmentArray"]=array();
	//转向展示页面
	echo "<script>alert('活动模板新增成功！');</script>";
	echo "<script>window.location.href='templateAction.php?op=query&tag=q';</script>";
}


//获取某一个模板的详细资料
if ($operation=="get") {
	$id				= $_GET["id"];
	$tag			= $_GET["tag"];
	$idName			= "activity_id";

	//查询模板信息
	$templateInfo		= Database::findById($table, $idName, $id);
	//查询附件信息
	$attachmentArray	= Database::find("select * from activity_attachment where activity_id=$id");

	//将取得的会员信息写入session
	$_SESSION["templateInfo"]			= $templateInfo;
	$_SESSION["attachmentArray"]		= $attachmentArray;
	
	//转向处理页面
	if($tag=="e"){
		echo "<script>window.location.href='../view/activity/templateEdit.php';</script>";
	}else if($tag=="q"){
		echo "<script>window.location.href='../view/activity/templateDetail.php';</script>";
	}else if($tag=="i"){
		echo "<script>window.location.href='../view/activity/instanceDetail.php?op=add';</script>";
	}
}

//修改模板的资料
if ($operation=="edit") {
	$id				= $_GET["id"];
	$subject		= $_POST["subject"];
	$travel			= $_POST["travel"];
	$fee			= $_POST["fee"];

	Database::begin();

	//更新活动模板资料
	$sql = "update info_activity set 
				subject		= '$subject',
				travel 		= '$travel',
				fee 		= '$fee'";
	$sql .= " where activity_id = $id";

	Database::update($sql);
	
	//附件更新采取的方式是：删除附件表该模板的所有附件，重新插入session中的附件数据
	$sql = "delete from activity_attachment where activity_id = $id";
	Database::delete($sql);
	if(!empty($_SESSION["attachmentArray"])){
		foreach ($_SESSION["attachmentArray"] as $attachmentInfo){
			$fileName = $attachmentInfo["file_name"];
			$filePath = $attachmentInfo["file_path"];
			$sql = "insert into activity_attachment(activity_id,file_name,file_path)"
					."VALUES ('$id','$fileName','$filePath')";
			Database::insert($sql);
		}
	}

	Database::commit();
	Database::end();
	//转向展示页面
	echo "<script>alert('活动模板修改成功！');</script>";
	echo "<script>window.location.href='templateAction.php?op=query&tag=e';</script>";
}

//删除活动模板的资料
if ($operation=="del") {
	$id				= $_GET["id"];


	Database::begin();

	//删除活动模板资料
	$sql = "delete from  info_activity where activity_id = $id";
	Database::delete($sql);
	$sql = "delete from  activity_attachment where activity_id = $id";
	Database::delete($sql);

	Database::commit();
	Database::end();
	//转向展示页面
	echo "<script>alert('活动模板删除成功！');</script>";
	echo "<script>window.location.href='templateAction.php?op=query&tag=e';</script>";
}
//关闭数据库
Database::close();
?>